package com.itrus.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.KeyStore;
import java.security.KeyStore.PasswordProtection;
import java.security.KeyStore.SecretKeyEntry;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.PrivateKey;
import java.security.Provider;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.security.Security;
import java.security.cert.Certificate;
import java.security.spec.AlgorithmParameterSpec;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.X509EncodedKeySpec;
import java.text.NumberFormat;
import java.util.HashMap;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.CipherOutputStream;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyAgreement;
import javax.crypto.KeyGenerator;
import javax.crypto.Mac;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import org.apache.log4j.Logger;
import org.bouncycastle.jce.provider.BouncyCastleProvider;

public class CipherUtils
{
  public static String AES;
  public static String Blowfish;
  public static String DES;
  public static String DSA;
  public static String DiffieHellman;
  public static String MD5;
  public static String RSA;
  public static String SHA1;
  public static String TripleDES;
  static Logger log = Logger.getLogger(CipherUtils.class);
  private static HashMap opmodes;
  private static String provider = "BC";
  private static int useDefaultKeysize;

  static
  {
    try
    {
      Class.forName("org.bouncycastle.jce.provider.BouncyCastleProvider");
      i = 1;
      if ((Security.getProvider("BC") == null) && (i != 0))
        Security.addProvider(new BouncyCastleProvider());
      AES = "AES";
      Blowfish = "Blowfish";
      DES = "DES";
      TripleDES = "DESede";
      MD5 = "HmacMD5";
      SHA1 = "HmacSHA1";
      useDefaultKeysize = -1;
      DSA = "DSA";
      RSA = "RSA";
      DiffieHellman = "DH";
      opmodes = new HashMap();
      opmodes.put("encrypt", new Integer(1));
      opmodes.put("decrypt", new Integer(2));
      return;
    }
    catch (ClassNotFoundException localClassNotFoundException)
    {
      while (true)
      {
        log.warn("org.bouncycastle.jce.provider.BouncyCastleProvider is not found.");
        provider = "SunJCE";
        int i = 0;
      }
    }
  }

  public static Key createSymmetricKey(String paramString)
    throws NoSuchAlgorithmException
  {
    if (paramString == null)
      paramString = "DES";
    SecureRandom localSecureRandom = new SecureRandom();
    KeyGenerator localKeyGenerator = KeyGenerator.getInstance(paramString);
    localKeyGenerator.init(localSecureRandom);
    return localKeyGenerator.generateKey();
  }

  public static byte[] decrypt(byte[] paramArrayOfByte, Key paramKey)
    throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalStateException, IllegalBlockSizeException, BadPaddingException
  {
    Cipher localCipher = Cipher.getInstance(paramKey.getAlgorithm());
    localCipher.init(2, paramKey);
    return localCipher.doFinal(paramArrayOfByte);
  }

  public static byte[] decryptWithCert(Certificate paramCertificate, byte[] paramArrayOfByte)
    throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException, NoSuchProviderException
  {
    return doFinal("decrypt", paramCertificate, paramArrayOfByte);
  }

  public static byte[] decryptWithKey(Key paramKey, byte[] paramArrayOfByte)
    throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException, NoSuchProviderException
  {
    return doFinal("decrypt", paramKey, paramArrayOfByte);
  }

  public static byte[] decryptWithPassword(byte[] paramArrayOfByte1, byte[] paramArrayOfByte2)
    throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidAlgorithmParameterException, NoSuchProviderException
  {
    return doFinal("decrypt", new SecretKeySpec(paramArrayOfByte1, "PBEWithMD5AndDES"), paramArrayOfByte2);
  }

  public static byte[] doFinal(String paramString, Object paramObject, InputStream paramInputStream, int paramInt, OutputStream paramOutputStream)
    throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException, IOException, NoSuchProviderException
  {
    byte[] arrayOfByte1 = (byte[])null;
    Cipher localCipher = initCipher(paramString, paramObject);
    CipherOutputStream localCipherOutputStream;
    byte[] arrayOfByte2;
    if (localCipher != null)
    {
      localCipherOutputStream = new CipherOutputStream(paramOutputStream, localCipher);
      if (paramInt < 1)
        paramInt = 2147483647;
      arrayOfByte2 = new byte[paramInt];
    }
    for (int i = 0; ; i++)
    {
      if (i >= paramInt);
      int j;
      while (true)
      {
        if (arrayOfByte1 == null)
        {
          NumberFormat localNumberFormat = NumberFormat.getInstance();
          log.error("Out of " + localNumberFormat.format(2147483647L) + " bytes.");
        }
        return arrayOfByte1;
        log.error("Initialize Cipher Faild!");
        return null;
        j = paramInputStream.read();
        if (j != -1)
          break;
        arrayOfByte1 = new byte[i];
        for (int k = 0; k < i; k++)
          arrayOfByte1[k] = arrayOfByte2[k];
      }
      arrayOfByte2[i] = ((byte)j);
      localCipherOutputStream.write(j);
    }
  }

  public static byte[] doFinal(String paramString, Object paramObject1, Object paramObject2, byte[] paramArrayOfByte)
    throws IllegalBlockSizeException, BadPaddingException, InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidAlgorithmParameterException, NoSuchProviderException
  {
    Cipher localCipher = initCipher(paramString, paramObject1, paramObject2);
    if (localCipher != null)
      return localCipher.doFinal(paramArrayOfByte);
    log.error("Initialize Cipher Faild!");
    return null;
  }

  public static byte[] doFinal(String paramString, Object paramObject, byte[] paramArrayOfByte)
    throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException, NoSuchProviderException
  {
    try
    {
      byte[] arrayOfByte = doFinal(paramString, paramObject, null, paramArrayOfByte);
      return arrayOfByte;
    }
    catch (InvalidAlgorithmParameterException localInvalidAlgorithmParameterException)
    {
      localInvalidAlgorithmParameterException.printStackTrace();
    }
    return null;
  }

  public static byte[] encrypt(byte[] paramArrayOfByte, Key paramKey)
    throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalStateException, IllegalBlockSizeException, BadPaddingException
  {
    Cipher localCipher = Cipher.getInstance(paramKey.getAlgorithm());
    localCipher.init(1, paramKey);
    return localCipher.doFinal(paramArrayOfByte);
  }

  public static byte[] encryptWithCert(Certificate paramCertificate, byte[] paramArrayOfByte)
    throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException, NoSuchProviderException
  {
    return doFinal("encrypt", paramCertificate, paramArrayOfByte);
  }

  public static byte[] encryptWithKey(Key paramKey, byte[] paramArrayOfByte)
    throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException, NoSuchProviderException
  {
    return doFinal("encrypt", paramKey, paramArrayOfByte);
  }

  public static byte[] encryptWithPassword(byte[] paramArrayOfByte1, byte[] paramArrayOfByte2)
    throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidAlgorithmParameterException, NoSuchProviderException
  {
    return doFinal("encrypt", new SecretKeySpec(paramArrayOfByte1, "PBEWithMD5AndDES"), paramArrayOfByte2);
  }

  public static SecretKey genCommunicationKey(String paramString, PublicKey paramPublicKey, PrivateKey paramPrivateKey)
    throws InvalidKeyException, IllegalStateException, NoSuchAlgorithmException, NoSuchProviderException
  {
    if (provider != null);
    for (KeyAgreement localKeyAgreement = KeyAgreement.getInstance(paramPublicKey.getAlgorithm(), provider); ; localKeyAgreement = KeyAgreement.getInstance(paramPublicKey.getAlgorithm()))
    {
      localKeyAgreement.init(paramPrivateKey);
      localKeyAgreement.doPhase(paramPublicKey, true);
      return localKeyAgreement.generateSecret(paramString);
    }
  }

  public static SecretKey genCommunicationKey(PublicKey paramPublicKey, PrivateKey paramPrivateKey)
    throws InvalidKeyException, IllegalStateException, NoSuchAlgorithmException, NoSuchProviderException
  {
    if (provider != null);
    for (KeyAgreement localKeyAgreement = KeyAgreement.getInstance(paramPublicKey.getAlgorithm(), provider); ; localKeyAgreement = KeyAgreement.getInstance(paramPublicKey.getAlgorithm()))
    {
      localKeyAgreement.init(paramPrivateKey);
      localKeyAgreement.doPhase(paramPublicKey, true);
      return localKeyAgreement.generateSecret(DES);
    }
  }

  public static KeyPair genCommunicationKeyPair()
  {
    try
    {
      KeyPair localKeyPair = genKeyPair(DiffieHellman);
      return localKeyPair;
    }
    catch (NoSuchAlgorithmException localNoSuchAlgorithmException)
    {
      log.debug(localNoSuchAlgorithmException.getLocalizedMessage());
      log.error("Generate Communication KeyPair Faild!");
      localNoSuchAlgorithmException.printStackTrace();
    }
    return null;
  }

  public static SecretKey genKey()
  {
    try
    {
      SecretKey localSecretKey = genKey(DES);
      return localSecretKey;
    }
    catch (NoSuchAlgorithmException localNoSuchAlgorithmException)
    {
      log.debug(localNoSuchAlgorithmException.getLocalizedMessage());
      log.error("Generate default algorthm(DES) SecretKey Faild!");
      localNoSuchAlgorithmException.printStackTrace();
    }
    return null;
  }

  public static SecretKey genKey(String paramString)
    throws NoSuchAlgorithmException
  {
    return genKey(paramString, null);
  }

  public static SecretKey genKey(String paramString1, int paramInt, String paramString2, Object paramObject)
    throws NoSuchAlgorithmException, NoSuchProviderException
  {
    KeyGenerator localKeyGenerator;
    SecureRandom localSecureRandom;
    if (paramString2 == null)
      if (provider != null)
      {
        localKeyGenerator = KeyGenerator.getInstance(paramString1, provider);
        localSecureRandom = useSecureRandom(paramObject);
        if ((paramInt <= 0) || (localSecureRandom == null))
          break label67;
        localKeyGenerator.init(paramInt, localSecureRandom);
      }
    while (true)
    {
      return localKeyGenerator.generateKey();
      localKeyGenerator = KeyGenerator.getInstance(paramString1);
      break;
      localKeyGenerator = KeyGenerator.getInstance(paramString1, paramString2);
      break;
      label67: if (paramInt > 0)
        localKeyGenerator.init(paramInt);
      if (localSecureRandom != null)
        localKeyGenerator.init(localSecureRandom);
    }
  }

  public static SecretKey genKey(String paramString, int paramInt, SecureRandom paramSecureRandom)
    throws NoSuchAlgorithmException
  {
    if (paramSecureRandom != null);
    try
    {
      return genKey(paramString, paramInt, null, paramSecureRandom);
      SecretKey localSecretKey = genKey(paramString, paramInt, null, Boolean.valueOf(false));
      return localSecretKey;
    }
    catch (NoSuchProviderException localNoSuchProviderException)
    {
      log.debug(localNoSuchProviderException.getLocalizedMessage());
      log.error("Unknown error!");
      localNoSuchProviderException.printStackTrace();
    }
    return null;
  }

  public static SecretKey genKey(String paramString, SecureRandom paramSecureRandom)
    throws NoSuchAlgorithmException
  {
    return genKey(paramString, useDefaultKeysize, paramSecureRandom);
  }

  public static KeyPair genKeyPair()
  {
    try
    {
      KeyPair localKeyPair = genKeyPair(RSA);
      return localKeyPair;
    }
    catch (NoSuchAlgorithmException localNoSuchAlgorithmException)
    {
      log.debug(localNoSuchAlgorithmException.getLocalizedMessage());
      log.error("Generate default algorthm(RSA) KeyPair Faild!");
      localNoSuchAlgorithmException.printStackTrace();
    }
    return null;
  }

  public static KeyPair genKeyPair(String paramString)
    throws NoSuchAlgorithmException
  {
    return genKeyPair(paramString, 1024);
  }

  public static KeyPair genKeyPair(String paramString, int paramInt)
    throws NoSuchAlgorithmException
  {
    return genKeyPair(paramString, paramInt, null);
  }

  public static KeyPair genKeyPair(String paramString1, int paramInt, String paramString2, Object paramObject)
    throws NoSuchAlgorithmException, NoSuchProviderException
  {
    if (paramInt < 0)
      return null;
    KeyPairGenerator localKeyPairGenerator;
    if (paramString2 == null)
      if (provider != null)
      {
        localKeyPairGenerator = KeyPairGenerator.getInstance(paramString1, provider);
        SecureRandom localSecureRandom = useSecureRandom(paramObject);
        if (localSecureRandom == null)
          break label69;
        localKeyPairGenerator.initialize(paramInt, localSecureRandom);
      }
    while (true)
    {
      return localKeyPairGenerator.genKeyPair();
      localKeyPairGenerator = KeyPairGenerator.getInstance(paramString1);
      break;
      localKeyPairGenerator = KeyPairGenerator.getInstance(paramString1, paramString2);
      break;
      label69: localKeyPairGenerator.initialize(paramInt);
    }
  }

  public static KeyPair genKeyPair(String paramString, int paramInt, SecureRandom paramSecureRandom)
    throws NoSuchAlgorithmException
  {
    if (paramSecureRandom != null);
    try
    {
      return genKeyPair(paramString, paramInt, null, paramSecureRandom);
      KeyPair localKeyPair = genKeyPair(paramString, paramInt, null, Boolean.valueOf(false));
      return localKeyPair;
    }
    catch (NoSuchProviderException localNoSuchProviderException)
    {
      log.debug(localNoSuchProviderException.getLocalizedMessage());
      log.error("Unknown error!");
      localNoSuchProviderException.printStackTrace();
    }
    return null;
  }

  public static PublicKey getCommunicationPublicKey(byte[] paramArrayOfByte)
    throws NoSuchProviderException, InvalidKeySpecException
  {
    X509EncodedKeySpec localX509EncodedKeySpec = new X509EncodedKeySpec(paramArrayOfByte);
    try
    {
      if (provider != null);
      KeyFactory localKeyFactory;
      for (Object localObject = KeyFactory.getInstance(DiffieHellman, provider); ; localObject = localKeyFactory)
      {
        return ((KeyFactory)localObject).generatePublic(localX509EncodedKeySpec);
        localKeyFactory = KeyFactory.getInstance(DiffieHellman);
      }
    }
    catch (NoSuchAlgorithmException localNoSuchAlgorithmException)
    {
      localNoSuchAlgorithmException.printStackTrace();
    }
    return null;
  }

  public static String getProvider()
  {
    return provider;
  }

  public static String[] getProviderNames()
  {
    Provider[] arrayOfProvider = Security.getProviders();
    String[] arrayOfString = new String[arrayOfProvider.length];
    for (int i = 0; ; i++)
    {
      if (i >= arrayOfProvider.length)
        return arrayOfString;
      arrayOfString[i] = arrayOfProvider[i].getName();
    }
  }

  public static SecretKey getSecretKeyFromFile(String paramString1, char[] paramArrayOfChar, String paramString2)
  {
    File localFile = new File(paramString1);
    if (!localFile.exists())
    {
      log.error("File not exists!");
      return null;
    }
    try
    {
      KeyStore localKeyStore = KeyStore.getInstance("JCEKS");
      localKeyStore.load(new FileInputStream(localFile), paramArrayOfChar);
      SecretKey localSecretKey = (SecretKey)localKeyStore.getKey(paramString2, paramArrayOfChar);
      return localSecretKey;
    }
    catch (Exception localException)
    {
      log.debug(localException.getLocalizedMessage());
      log.error("Some error in loading secretkey!");
      localException.printStackTrace();
    }
    return null;
  }

  private static Cipher initCipher(String paramString, Object paramObject)
    throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException, NoSuchProviderException
  {
    try
    {
      Cipher localCipher = initCipher(paramString, paramObject, null);
      return localCipher;
    }
    catch (InvalidAlgorithmParameterException localInvalidAlgorithmParameterException)
    {
      log.debug(localInvalidAlgorithmParameterException.getLocalizedMessage());
      log.error("Unknown error!");
      localInvalidAlgorithmParameterException.printStackTrace();
    }
    return null;
  }

  private static Cipher initCipher(String paramString, Object paramObject1, Object paramObject2)
    throws IllegalBlockSizeException, BadPaddingException, InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidAlgorithmParameterException, NoSuchProviderException
  {
    Cipher localCipher = null;
    int i = ((Integer)opmodes.get(paramString.toLowerCase())).intValue();
    boolean bool1 = paramObject1 instanceof Key;
    String str = null;
    Key localKey = null;
    if (bool1)
    {
      localKey = (Key)paramObject1;
      str = localKey.getAlgorithm();
      if ((str.endsWith(MD5)) || (str.equals(SHA1)))
        log.error("HmacMD5 and HmacSHA1 Key used for MAC only!");
    }
    label216: label225: 
    while (true)
    {
      return localCipher;
      boolean bool2 = paramObject1 instanceof Certificate;
      Certificate localCertificate = null;
      if (bool2)
      {
        localCertificate = (Certificate)paramObject1;
        PublicKey localPublicKey = localCertificate.getPublicKey();
        if (localPublicKey == null)
        {
          log.error("Can't find PublicKey form Certificate.");
          return null;
        }
        str = localPublicKey.getAlgorithm();
      }
      boolean bool3 = paramObject2 instanceof AlgorithmParameterSpec;
      AlgorithmParameterSpec localAlgorithmParameterSpec = null;
      if (bool3)
        localAlgorithmParameterSpec = (AlgorithmParameterSpec)paramObject2;
      if (provider != null)
      {
        localCipher = Cipher.getInstance(str, provider);
        if (localKey != null)
        {
          if (localAlgorithmParameterSpec == null)
            break label216;
          localCipher.init(i, localKey, localAlgorithmParameterSpec);
        }
      }
      while (true)
      {
        if (localCertificate == null)
          break label225;
        localCipher.init(i, localCertificate);
        return localCipher;
        localCipher = Cipher.getInstance(str);
        break;
        localCipher.init(i, localKey);
      }
    }
  }

  public static byte[] mac(SecretKey paramSecretKey, byte[] paramArrayOfByte)
    throws InvalidKeyException, NoSuchAlgorithmException, NoSuchProviderException
  {
    if (provider != null);
    for (Mac localMac = Mac.getInstance(paramSecretKey.getAlgorithm(), provider); ; localMac = Mac.getInstance(paramSecretKey.getAlgorithm()))
    {
      localMac.init(paramSecretKey);
      return localMac.doFinal(paramArrayOfByte);
    }
  }

  public static String md5(String paramString)
  {
    try
    {
      String str = md5(paramString.getBytes("GBK"));
      return str;
    }
    catch (UnsupportedEncodingException localUnsupportedEncodingException)
    {
    }
    return null;
  }

  public static String md5(byte[] paramArrayOfByte)
  {
    try
    {
      MessageDigest localMessageDigest = MessageDigest.getInstance("MD5");
      localMessageDigest.update(paramArrayOfByte);
      return DERUtils.BytesToHexString(localMessageDigest.digest());
    }
    catch (NoSuchAlgorithmException localNoSuchAlgorithmException)
    {
    }
    throw new IllegalStateException("System doesn't support MD5 algorithm.");
  }

  public static Key recoverKeyObjectFromFile(String paramString)
    throws FileNotFoundException, IOException, ClassNotFoundException
  {
    ObjectInputStream localObjectInputStream = new ObjectInputStream(new FileInputStream(paramString));
    Key localKey = (Key)localObjectInputStream.readObject();
    localObjectInputStream.close();
    return localKey;
  }

  public static Key retrieveKey(byte[] paramArrayOfByte, String paramString)
  {
    if (paramString == null)
      paramString = "DES";
    return new SecretKeySpec(paramArrayOfByte, paramString);
  }

  public static KeyStore saveSecretKey2File(String paramString1, char[] paramArrayOfChar, String paramString2, SecretKey paramSecretKey)
  {
    File localFile = new File(paramString1);
    try
    {
      KeyStore localKeyStore = KeyStore.getInstance("JCEKS");
      if (localFile.exists())
        localKeyStore.load(new FileInputStream(localFile), paramArrayOfChar);
      while (true)
      {
        localKeyStore.setEntry(paramString2, new KeyStore.SecretKeyEntry(paramSecretKey), new KeyStore.PasswordProtection(paramArrayOfChar));
        localKeyStore.store(new FileOutputStream(localFile), paramArrayOfChar);
        return localKeyStore;
        localKeyStore.load(null, paramArrayOfChar);
      }
    }
    catch (Exception localException)
    {
      log.debug(localException.getLocalizedMessage());
      log.error("Some error in saving secretkey!");
      localException.printStackTrace();
    }
    return null;
  }

  public static void serializeKeyObjectToFile(String paramString1, String paramString2)
    throws NoSuchAlgorithmException, IOException
  {
    Key localKey = createSymmetricKey(paramString2);
    ObjectOutputStream localObjectOutputStream = new ObjectOutputStream(new FileOutputStream(paramString1));
    localObjectOutputStream.writeObject(localKey);
    localObjectOutputStream.close();
  }

  public static void setProvider(String paramString)
  {
    provider = paramString;
  }

  public static String sha1(String paramString)
  {
    return sha1(paramString.getBytes());
  }

  public static String sha1(byte[] paramArrayOfByte)
  {
    try
    {
      MessageDigest localMessageDigest = MessageDigest.getInstance("SHA1");
      localMessageDigest.update(paramArrayOfByte);
      return DERUtils.BytesToHexString(localMessageDigest.digest());
    }
    catch (NoSuchAlgorithmException localNoSuchAlgorithmException)
    {
    }
    throw new IllegalStateException("System doesn't support SHA1 algorithm.");
  }

  private static SecureRandom useSecureRandom(Object paramObject)
  {
    SecureRandom localSecureRandom;
    if (paramObject == null)
      localSecureRandom = null;
    do
    {
      return localSecureRandom;
      boolean bool1 = paramObject instanceof Boolean;
      localSecureRandom = null;
      if (bool1)
      {
        boolean bool2 = ((Boolean)paramObject).booleanValue();
        localSecureRandom = null;
        if (bool2)
          localSecureRandom = new SecureRandom();
      }
    }
    while (!(paramObject instanceof SecureRandom));
    return (SecureRandom)paramObject;
  }
}

/* Location:           C:\Users\user\Desktop\dd28_pcdd720\classes_dex2jar.jar
 * Qualified Name:     com.itrus.util.CipherUtils
 * JD-Core Version:    0.6.2
 */